package com.example.administrator.mobliesafe;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.administrator.mobilesafe.utils.BaseSetupAcitivity;

public class Setup3Activity extends BaseSetupAcitivity {
    private Button bt_srtup3_pre;
    private Button bt_srtup3_next;
    private Button selectContactNumber;
    private String number;
    private EditText et_number;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setup3);
        sharedPreferences = getSharedPreferences("config",MODE_PRIVATE);
        selectContactNumber = (Button) findViewById(R.id.select_contact_number);
        bt_srtup3_next = (Button) findViewById(R.id.bt_setup2_next);
        bt_srtup3_pre = (Button) findViewById(R.id.bt_setup2_pre);
        et_number = (EditText) findViewById(R.id.et_number);
        if(!TextUtils.isEmpty(sharedPreferences.getString("number",null))){
            et_number.setText(sharedPreferences.getString("number",null));
        }
        bt_srtup3_pre.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pre(v);
            }
        });
        bt_srtup3_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                number = et_number.getText().toString().trim();
                if (TextUtils.isEmpty(number)) {
                    Toast.makeText(Setup3Activity.this,"请设置安全号码",Toast.LENGTH_SHORT).show();
                    return;
                }
                next(v);
            }
        });
        selectContactNumber.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Setup3Activity.this, SelectContactNumberActivity.class);
                startActivityForResult(intent,0);
            }
        });
    }

    @Override
    public void showPre() {
        Intent intent = new Intent(Setup3Activity.this,Setup2Activity.class);
        startActivity(intent);
        finish();
        overridePendingTransition(R.anim.tran_in, R.anim.tran_out);
    }

    @Override
    public void showNext() {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("number",number);
        editor.commit();
        Intent intent = new Intent(Setup3Activity.this,Setup4Activity.class);
        startActivity(intent);
        finish();
        overridePendingTransition(R.anim.tran_out, R.anim.tran_in);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(data == null){
            return;
        }
        number = data.getStringExtra("number").toString();
        et_number.setText(number);
    }
}
